package com.w7vm.menuweb.entity;

import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the AcessoWeb database table.
 * 
 */
@Entity
@NamedQueries({
	@NamedQuery(name="Acesso.findRestauranteAcessoByUsuario", 
					query="SELECT a" +
					" FROM AcessoWeb a" +
					" JOIN a.usuarioWeb u" +
					" JOIN a.restauranteSedeWeb r " +
					" WHERE u.username = :username"),
					
	// Perfil ADM CONTA
	@NamedQuery(name="Acesso.findUsuariosAllByIDRestaurante",
					query="SELECT a" +
					" FROM AcessoWeb a" +
					" JOIN a.usuarioWeb u" +
					" JOIN a.restauranteSedeWeb r " +
					" WHERE r.idRestauranteSedeWeb = :idRestaurante"),

	// Perfil ADM RESTAURANTE -- Procura SEDE que o usuário ADM tem acesso
	@NamedQuery(name="Acesso.findUsuariosSedeByIDRestaurante",
					query="SELECT a " +
							" FROM AcessoWeb a" +
							" JOIN a.restauranteSedeWeb r" +
							" WHERE r.idRestauranteSedeWeb = :idRestaurante"+
							" AND a.perfil = 'Usuario'" +
							" AND a.acessoFilial = 'N'"),

	// Perfil ADM RESTAURANTE -- Procura Filial que o usuário ADM esta relacionado
	@NamedQuery(name="Acesso.findUsuariosFiliaisByIDRestaurante",
					query="SELECT a" +
							" FROM AcessoWeb a" +
							" JOIN a.usuarioWeb u" +
							" JOIN a.restauranteSedeWeb r" +
							" JOIN a.restauranteFilialWeb f"+
							" WHERE r.idRestauranteSedeWeb = :idRestaurante" +
							" AND a.perfil = 'USUARIO'" +
							" AND f.idRestauranteFilialWeb IN (SELECT rf.idRestauranteFilialWeb" +
							" FROM AcessoWeb c" +
							" JOIN c.restauranteFilialWeb rf" +
							" JOIN c.usuarioWeb us" +
							" WHERE us.idUsuarioWeb = :idUsuario)"),
							

	//Lista Usuarios Perfil USUARIO relacionados ao restaurante do usuario ADM_RESTAURANTE				
	@NamedQuery(name="Acesso.findUsuarioByIDRestaurante",
					query="SELECT Distinct u.username" +
					" FROM AcessoWeb a" +
					" JOIN a.usuarioWeb u" +
					" JOIN a.restauranteSedeWeb r " +
					" WHERE r.idRestauranteSedeWeb = :idRestaurante" +
					" AND u.status = 1" +
					" AND a.perfil = 'USUARIO'"),


	//Perfil USUARIO				
	@NamedQuery(name="Acesso.findUsuarioByIDUsuario",
					query="SELECT a" +
					" FROM AcessoWeb a" +
					" JOIN a.usuarioWeb u" +
					" JOIN a.restauranteSedeWeb r " +
					" WHERE u.username = :username"),
					
	//Lista Permissão de Acesso do Usuario com Perfil ADM_RESTAURANTE				
	@NamedQuery(name="Acesso.findUsuarioAcessoByIDUsuario",
									query="SELECT a" +
									" FROM AcessoWeb a" +
									" JOIN a.usuarioWeb u" +
									" WHERE u.idUsuarioWeb = :idUsuario" +
									" AND a.status = 1"),
	
	//RESTAURANTE POR PERMISSÃO DE ACESSO DO USUARIO 				
	@NamedQuery(name="Acesso.findRestauranteByIDUsuario",
					query="SELECT a" +
					" FROM AcessoWeb a" +
					" JOIN a.usuarioWeb u" +
					" JOIN a.restauranteSedeWeb r " +
					" WHERE u.idUsuarioWeb = :idUsuario " +
					" AND a.acessoFilial = 'N'"),
								
	
	//Listar os acessos (filiais) que os usuários (todos os perfil) tem permissão
	@NamedQuery(name="Acesso.findAcessosByIDUsuario",
					query="SELECT a" +
					" FROM AcessoWeb a" +
					" JOIN a.usuarioWeb u" +
					" JOIN a.restauranteFilialWeb f" +
					" WHERE u.idUsuarioWeb = :idUsuario" +
					" ORDER BY u.username"),

									
})				

public class AcessoWeb implements Serializable {
	private static final long serialVersionUID = 1L;
	
	public static final String FIND_RESTAURANTEACESSO_BY_EMAIL = "Acesso.findRestauranteAcessoByUsuario";

	public static final String FIND_USUARIOSALL_BY_CODRESTAURANTE = "Acesso.findUsuariosAllByIDRestaurante";
	
	public static final String FIND_USUARIOSSEDE_BY_CODRESTAURANTE = "Acesso.findUsuariosSedeByIDRestaurante";
	public static final String FIND_USUARIOSFILIAIS_BY_CODRESTAURANTE = "Acesso.findUsuariosFiliaisByIDRestaurante";

	public static final String FIND_USUARIO_BY_CODUSUARIO = "Acesso.findUsuarioByIDUsuario";
	public static final String FIND_USUARIO_BY_CODRESTAURANTE = "Acesso.findUsuarioByIDRestaurante";

	public static final String FIND_RESTAURANTE_BY_CODUSUARIO = "Acesso.findRestauranteByIDUsuario";

	public static final String FIND_TIPOACESSO_BY_CODUSUARIO = "Acesso.findAcessosByIDUsuario";
	
	public static final String FIND_USUARIOACESSO_BY_CODUSUARIO = "Acesso.findUsuarioAcessoByIDUsuario"; 


	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int idAcessoWeb;

	@Column(name="AcessoFilial")
	private String acessoFilial;

	@Column(name="Perfil")
	private String perfil;

	@Column(name="Status")
	private int status;

	//bi-directional many-to-one association to RestauranteFilialWeb
    @ManyToOne
	@JoinColumn(name="idRestauranteFilial")
	private RestauranteFilialWeb restauranteFilialWeb;

	//bi-directional many-to-one association to RestauranteSedeWeb
//    @ManyToOne
//	@JoinColumn(name="idRestauranteSede")
//	private RestauranteSedeWeb restauranteSedeWeb;

	//bi-directional many-to-one association to UsuarioWeb
    @ManyToOne
	@JoinColumn(name="idUsuario")
	private UsuarioWeb usuarioWeb;

	//bi-directional many-to-one association to RestauranteSedeWeb
    @ManyToOne
	@JoinColumn(name="idRestauranteSede")
	private RestauranteSedeWeb restauranteSedeWeb;

    public AcessoWeb() {
    }

	public int getIdAcessoWeb() {
		return this.idAcessoWeb;
	}

	public void setIdAcessoWeb(int idAcessoWeb) {
		this.idAcessoWeb = idAcessoWeb;
	}

	public String getAcessoFilial() {
		return this.acessoFilial;
	}

	public void setAcessoFilial(String acessoFilial) {
		this.acessoFilial = acessoFilial;
	}

	public String getPerfil() {
		return this.perfil;
	}

	public void setPerfil(String perfil) {
		this.perfil = perfil;
	}

	public int getStatus() {
		return this.status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public RestauranteFilialWeb getRestauranteFilialWeb() {
		return this.restauranteFilialWeb;
	}

	public void setRestauranteFilialWeb(RestauranteFilialWeb restauranteFilialWeb) {
		this.restauranteFilialWeb = restauranteFilialWeb;
	}
	
	public RestauranteSedeWeb getRestauranteSedeWeb() {
		return this.restauranteSedeWeb;
	}

	public void setRestauranteSedeWeb(RestauranteSedeWeb restauranteSedeWeb) {
		this.restauranteSedeWeb = restauranteSedeWeb;
	}
	
	public UsuarioWeb getUsuarioWeb() {
		return this.usuarioWeb;
	}

	public void setUsuarioWeb(UsuarioWeb usuarioWeb) {
		this.usuarioWeb = usuarioWeb;
	}
	
}